package cn.ticket.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Busorder;
import cn.ticket.service.BusOrderService;
import cn.ticket.service.BusesService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("busOrder")
public class BusOrderController {
@Autowired
private BusOrderService busOrderService;
@Autowired
private BusesService busesService;
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Busorder record){
	Map<String, Object> result=new HashMap<>();
	int countShould=5;
	if (record.getOrientation()==0) {
		record.setSeatscount((short) 235);
		record.setBusescount((byte) 5);
	}
	else {
		record.setSeatscount((short) 235);
		record.setBusescount((byte) 5);
		countShould=5;
	}
	int num=busOrderService.insertSelective(record);	
	if (num==1) {		
		int count=0;
		if (record.getOrientation()==0) {
			count=busesService.initialInsert0(record.getId());
		}
		else if (record.getOrientation()==1) {
			count=busesService.initialInsert1(record.getId());
		}
		if (count==countShould) {
			result.put("status", "1001");
		}
		else {
			result.put("status", "1003");
		}
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String update(@RequestBody Busorder record){
	Map<String, Object> result=new HashMap<>();
	int num=busOrderService.updateByPrimaryKeySelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="delete",produces={"application/json;charset=utf-8"})
@ResponseBody
public String update(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));
	int num=busOrderService.deleteByPrimaryKey(id);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> result=new HashMap<>();
	List<Busorder> busorders=busOrderService.getAll();
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getById",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getById(@RequestBody Map<String, Object> args){
	Map<String, Object> result=new HashMap<>();
	Integer id=(Integer) args.get("id");
	Busorder busorder=busOrderService.selectByPrimaryKey(id);
	if (busorder!=null) {
		result.put("status", 1001);
		result.put("body", busorder);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getNearestBusorder0",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getNearestBusorder0(){
	Map<String, Object> result=new HashMap<>();
	List<Busorder> busorders=busOrderService.getValideOrder0();
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders.get(0));
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getValidateOrders",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getValidateOrders(){
	Map<String, Object> result=new HashMap<>();
	List<Busorder> busorders=busOrderService.getValidateOrders();
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getInvalidateOrders",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getInvalidateOrders(){
	Map<String, Object> result=new HashMap<>();
	List<Busorder> busorders=busOrderService.getInvalidateOrders();
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getNearestBusorder1",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getNearestBusorder1(){
	Map<String, Object> result=new HashMap<>();
	List<Busorder> busorders=busOrderService.getValideOrder1();
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders.get(0));
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByCount(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int start=Integer.parseInt(args.get("start"));
	int count=Integer.parseInt(args.get("count"));
	List<Busorder> busorders=busOrderService.getByCount(start, count);
	if (!busorders.isEmpty()) {
		result.put("status", 1001);
		result.put("body", busorders);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
}
